﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for AdsControl.xaml
    /// </summary>
    public partial class SpiderNode : UserControl
    {
        #region Properties
        public string Headline
        {
            get { return (string)GetValue(HeadlineProperty); }
            set { SetValue(HeadlineProperty, value); }
        }
        public string SubHeadline
        {
            get { return (string)GetValue(SubHeadlineProperty); }
            set { SetValue(SubHeadlineProperty, value); }
        }

        public string ImageUrl
        {
            get { return (string)GetValue(ImageUrlProperty); }
            set { SetValue(ImageUrlProperty, value); }
        }


        public string PubDate
        {
            get { return (string)GetValue(PubDateProperty); }
            set { SetValue(PubDateProperty, value); }
        }
        //public NewsArticle Article
        //{

        //    get { return (NewsArticle)GetValue(ArticleProperty); }
        //    set
        //    {
        //        SetValue(ArticleProperty, value);
        //        if (value != null)
        //        {
        //            Headline = value.Headline;
        //            SubHeadline = value.SubHeadline;
        //            ImageUrl = value.ImageUrl;
        //            PubDate = value.PubDate;
        //        }
        //    }
        //}



        public NewsArticle Article
        {
            get { return (NewsArticle)GetValue(ArticleProperty); }
            set { SetValue(ArticleProperty, value); }
        }



        #endregion

        #region Dependency Properties
        public static readonly DependencyProperty HeadlineProperty = DependencyProperty.Register("Headline", typeof(string), typeof(SpiderNode));
        public static readonly DependencyProperty SubHeadlineProperty = DependencyProperty.Register("SubHeadline", typeof(string), typeof(SpiderNode));
        public static readonly DependencyProperty PubDateProperty = DependencyProperty.Register("PubDate", typeof(string), typeof(SpiderNode));
        public static readonly DependencyProperty ImageUrlProperty =
            DependencyProperty.Register("ImageUrl", typeof(string), typeof(SpiderNode), new PropertyMetadata(OnImageUrlChanged));
        public static readonly DependencyProperty ArticleProperty =
            DependencyProperty.Register("Article", typeof(NewsArticle), typeof(SpiderNode), new UIPropertyMetadata(OnArticlePropertyChanged));

        #endregion

        #region Events
        public event MouseButtonEventHandler Click;
        #endregion

        #region Ctor
        public SpiderNode()
        {
            InitializeComponent();
            borderArticle.MouseDown += new MouseButtonEventHandler(gridLayout_MouseDown);
        }


        #endregion

        #region Event Handlers
        void gridLayout_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
        #endregion

        #region Dependency Handlers

        private static void OnImageUrlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SpiderNode).OnImageUrlChanged(e);
        }

        private static void OnArticlePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SpiderNode).OnArticlePropertyChanged(e);
        }

        private void OnArticlePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if ((NewsArticle)e.NewValue != null)
            {
                NewsArticle article = (NewsArticle)e.NewValue;
                Headline = article.Headline;
                SubHeadline = article.SubHeadline;
                ImageUrl = article.ImageUrl;
                PubDate = article.PubDate;
            }
        }


        private void OnImageUrlChanged(DependencyPropertyChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.NewValue.ToString()))
            {
                txtSubHeadline.SetValue(Grid.ColumnSpanProperty, 2);
                imgArticle.Visibility = Visibility.Hidden;
            }
            else
            {
                txtSubHeadline.SetValue(Grid.ColumnSpanProperty, 1);
                imgArticle.Visibility = Visibility.Visible;
            }
        }
        #endregion
    }
}

